﻿using System;
using System.Reflection;
using System.Configuration;

using SBPweb.Communication.Shared.Library.Contacts;
using SBPweb.Communication.Shared.Library.Connections;
using SBPweb.Communication.Shared.Library.Conversations;
using SBPweb.Communication.Shared.Library.Profiles;

using SBPweb.Communication.Client.Library.Xmpp.Contacts;
using SBPweb.Communication.Client.Library.Xmpp.Connections;
using SBPweb.Communication.Client.Library.Xmpp.Conversations;
using SBPweb.Communication.Client.Library.Xmpp.Profiles;

namespace SBPweb.Communication.Client.Library.Xmpp.Configuration
{

    public class ConfigurationSection : System.Configuration.ConfigurationSection
    {

        #region Constructor logic

        private ConfigurationSection()
        {
        }

        internal static ConfigurationSection OpenApplicationConfiguration(Assembly asm)
        {
            Type type = typeof(ConfigurationSection);
            if (asm != null)
            {
                System.Configuration.Configuration c = System.Configuration.ConfigurationManager.OpenExeConfiguration(asm.Location);
                string sectionName = null;
                foreach (System.Configuration.ConfigurationSection cs in c.Sections)
                {
                    if (Type.GetType(cs.SectionInformation.Type) == type)
                    {
                        sectionName = cs.SectionInformation.Name;
                        break;
                    }
                }

                if (string.IsNullOrEmpty(sectionName))
                {
                    throw new ConfigurationErrorsException(string.Format("Could not find an applicable section derived from {0}.", type));
                }

                return c.GetSection(sectionName) as ConfigurationSection;
            }
            else
            {
                return System.Configuration.ConfigurationManager.GetSection("xmppConfiguration") as ConfigurationSection;
            }
        }

        #endregion

        #region Property accessors

        public Version Version
        {
            get
            {
                return new Version(version);
            }
        }

        public IConnectionManagerConfiguration Connections
        {
            get
            {
                return connections;
            }
        }

        public IContactManagerConfiguration Contacts
        {
            get
            {
                return contacts;
            }
        }

        public IConversationManagerConfiguration Conversations
        {
            get
            {
                return conversations;
            }
        }

        public IProfileManagerConfiguration Profiles
        {
            get
            {
                return profiles;
            }
        }


        [ConfigurationProperty("version", IsRequired = true)]
        protected string version
        {
            get
            {
                return (string)this["version"];
            }
        }

        [ConfigurationProperty("connectionManager", IsRequired = true)]
        protected ConnectionManagerConfigurationElement connections
        {
            get
            {
                return (ConnectionManagerConfigurationElement)this["connectionManager"];
            }
        }

        [ConfigurationProperty("contactManager", IsRequired = true)]
        protected ContactManagerConfigurationElement contacts
        {
            get
            {
                return (ContactManagerConfigurationElement)this["contactManager"];
            }
        }

        [ConfigurationProperty("conversationManager", IsRequired = true)]
        protected ConversationManagerConfigurationElement conversations
        {
            get
            {
                return (ConversationManagerConfigurationElement)this["conversationManager"];
            }
        }

        [ConfigurationProperty("profileManager", IsRequired = true)]
        protected ProfileManagerConfigurationElement profiles
        {
            get
            {
                return (ProfileManagerConfigurationElement)this["profileManager"];
            }
        }

        #endregion

    }

}